define(["moment"], function (moment) {
    return ["$scope", "$filter", "$stateParams", "config", "MMWService", "$calculater",
    function ($scope, $filter, $stateParams, config, $MMWService, $calculater) {
        $scope.init = function () {
            $scope.work_type = "1";//1 入库，0 发料
            $scope.workItemList = [];
            $scope.isSelectAll = false;
            $scope.PageConfig = { page: 0, page_count: 25 };
            $scope.dispatch_filter = {};
            $scope.order_by_mode = { name: "", sort: false };
            //$scope.C00_require_condition = true;
            $scope.codeList = [
                { name: $filter('translate')('B0238.queryList.column01'), column_name: 'a.AccessoryNo', column_query: loadAccInfo },
                { name: $filter('translate')('B0238.queryList.column02'), column_name: 'a.AccessoryType', column_query: loadAccInfo },
                { name: $filter('translate')('B0238.list.current_residual_life'), column_name: 'B.REPAIRCYCLE - a.ACCUMULATEQTY', data_type: 'N' }
            ];
        };

        $scope.qrcode_accessory_no = function () {
            $scope.$root.OpenScanner({
                after: function (result) {
                    $scope.accessory_no = result.text;
                    $scope.barcode_accessory_no();
                },
                exception: function () {
                    //目前裝置無法使用相機
                    $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                },
                orientation: 'landscape'
            });
        };
        $scope.barcode_accessory_no = function (event) {
            if (!event || event.keyCode == 13) {
                if ($scope.workItemList.length>0) {
                    for (var i = 0; i < $scope.workItemList.length; i++) {
                        if ($scope.workItemList[i].acc_no.indexOf($scope.accessory_no) != -1) {
                            $scope.workItemList[i].btn_mode = true;
                        }
                    }
                }
                $scope.accessory_no = "";
                $scope.$root.focusById("accessory_no");
            }
        };

        // 取得模治具資料
        function loadAccInfo(item) {
            // 20210203 modify by 奕達 for #86225 - start
            // 0: 發料 ， 1: 入庫
            if($scope.work_type == '0') {
                $MMWService.showQueryListPNIG({
                    page_mode: 'injection_mould_material_info_get',
                    pageRecord: 25,
                    condition: [
                        { 
                            name: $filter('translate')('B0238.queryList.column01'), 
                            column_name: item.column_name, 
                            query_mode: item.query_mode, 
                            value: '' 
                        }
                    ],
                    selectCode: item.value,
                    header: [
                        { 
                            label: $filter('translate')('B0238.queryList.column01'), 
                            code: 'mold_no' 
                        },
                        { 
                            label: $filter('translate')('B0238.queryList.column02'), 
                            code: 'mold_type' 
                        },
                        { 
                            label: $filter('translate')('B0238.queryList.column03'), 
                            code: 'mold_class' },
                        { 
                            label: $filter('translate')('B0238.queryList.column04'), 
                            code: 'cavity_standard_qty' 
                        },
                        { 
                            label: $filter('translate')('B0238.queryList.column05'), 
                            code: 'cavity_health_qty' 
                        }
                    ],
                    confirm: function (selectItem) {
                        item.value = item.column_name == 'a.AccessoryNo' ? selectItem.mold_no : selectItem.mold_type;
                    }
                });
            } else if ($scope.work_type == '1') {
                $MMWService.showQueryListPNIG({
                    page_mode: 'injection_mould_warehousing_info_get',
                    pageRecord: 25,
                    condition: [
                        { 
                            name: $filter('translate')('B0238.queryList.column01'), 
                            column_name: item.column_name, 
                            query_mode: item.query_mode, 
                            value: '' 
                        }
                    ],
                    selectCode: item.value,
                    header: [
                        { 
                            label: $filter('translate')('B0238.queryList.column01'), 
                            code: 'mold_no' 
                        },
                        { 
                            label: $filter('translate')('B0238.queryList.column02'), 
                            code: 'mold_type' 
                        },
                        { 
                            label: $filter('translate')('B0238.queryList.column03'), 
                            code: 'mold_class' },
                        { 
                            label: $filter('translate')('B0238.queryList.column04'), 
                            code: 'cavity_standard_qty' 
                        },
                        { 
                            label: $filter('translate')('B0238.queryList.column05'), 
                            code: 'cavity_health_qty' 
                        }
                    ],
                    confirm: function (selectItem) {
                        item.value = item.column_name == 'a.AccessoryNo' ? selectItem.mold_no : selectItem.mold_type;
                    }
                });
            }
            // 20210203 modify by 奕達 for #86225 - end
        };

        $scope.chengeType = function (type) {
            $scope.work_type = type;
            var condition = $scope.$root.genConditioninfo();
            $scope.search(condition);
        }

        $scope.back = function(){
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);
        };

        $scope.barcodeScan = function(event){
            if(event.keyCode == 13){
                var condition = $scope.$root.genConditioninfo();
                $scope.search(condition);
            }
        };

        $scope.search = function(condition){
            if($scope.input_value != ''){
                var content = {
                    type: $scope.work_type,
                    query_condition:condition
                };
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.acc_state_data_info_get',
                    content : content,
                    success : function(data){
                        if (data.details && data.details.length > 0) {
                            $scope.workItemList = data.details;
                            $scope.isSelectAll = false;
                        } else {
                            $scope.workItemList = [];
                            $scope.isSelectAll = false;
                            $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                        };
                    }
                });
            } else {
                $scope.$root.showAlert($filter('translate')('B0106.msg.column1', { title:$scope.title_item.query_name }));
            };
        };

        $scope.selectAll = function(){
            if($scope.workItemList && $scope.workItemList.length > 0){
                $scope.isSelectAll = !$scope.isSelectAll;
                $scope.workItemList.forEach(function(item){
                    if($scope.isSelectAll){
                        item.btn_mode = true;
                    } else {
                        item.btn_mode = false;
                    };
                });
            }
        };

        $scope.confirm = function(){
            var details = [];
            $scope.workItemList.forEach(function (obj) {
                if (obj.btn_mode) {
                    details.push(obj);
                }
            });
            if (details.length > 0) {
                var content = {
                    work_type: $scope.work_type,
                    details: details
                };
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.acc_state_change_process',
                    content: content,
                    success: function (data) {
                        $scope.$root.showToast($filter('translate')('common.msg.success'));
                        var condition = $scope.$root.genConditioninfo();
                        $scope.search(condition);
                    }
                });
            } else {
                $scope.$root.showAlert($filter('translate')('common.msg.plz_select_one'));
            };
        };

        $scope.orderbychange = function (mode) {
            //console.log($scope.order_by_mode);
            if ($scope.order_by_mode.name != mode) {
                $scope.order_by_mode.name = mode;
                $scope.order_by_mode.sort = false;
            }
            else {
                if ($scope.order_by_mode.sort)
                    $scope.order_by_mode.sort = false;
                else
                    $scope.order_by_mode.sort = true;
            }

        }
        //分頁
        $scope.getPages = function (list, PageConfig, filterItem) {
            if (list) {
                //console.log();
                if ($scope.order_by_mode.name != "")
                    list = $filter('orderBy')(list, $scope.order_by_mode.name, $scope.order_by_mode.sort);
                var temp_list = list.filter(function (item, index, array) {
                    var isOK = true,
                        keys = Object.keys(filterItem);
                    for (var i = 0; i < keys.length; i++) {
                        var key = keys[i];
                        if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                            if (item[key].toString().indexOf(filterItem[key]) == -1) {
                                isOK = false;
                                break;
                            }
                        }
                    }
                    return isOK;
                });
                if (PageConfig) {
                    if (temp_list.length != 0) {
                        PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);

                        if (PageConfig.page >= PageConfig.total_page) {
                            PageConfig.page = 0;
                        }
                        var index = PageConfig.page * PageConfig.page_count;
                        return temp_list.slice(index, index + PageConfig.page_count);
                    } else {
                        PageConfig.total_page = 1;
                        PageConfig.page = 0;
                    }
                } else {
                    return temp_list;
                }
            } else {

                return list;
            }
        };
    }
];});
